<template>
	<view class="p_b_128">
		<view class="p_over">
			<view class="list_wrapper" v-for="item in listData" :key="item.id" @click="getItem(item)">
				<view class="p_flex_2">
					<view class="p_font_28"><text class="p_bg_05b570 p_color_fff p_font_24 p_margin_r_8"
							style="line-height: 40rpx;padding: 0 8rpx;border-radius: 4rpx;"
							v-if="item.default == 1">默认</text>{{item.province}}{{item.city}}{{item.area}}{{item.address}}
					</view>
					<view class="p_font_24 p_color_7a p_margin_t_8">{{item.name}}<text
							class="p_margin_l_16">{{(item.phone).replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")}}</text>
					</view>
				</view>
				<view class="p_tb_8 p_margin_l_100">
					<image src="https://dy.sykj365.com/uploads/mini/xuanzhong@2x.png" class="p_img_46" v-if="item.id == id_"></image>
					<image src="https://dy.sykj365.com/uploads/mini/weixuanzhong@2x.png" class="p_img_46" v-else></image>
				</view>
			</view>
			<view class="p_font_24 p_color_7a p_text_c p_b_36" v-if="loadingText">{{loadingText}}</view>
		</view>
		<view class="p_bottom_fix p_bg_fff">
			<view class="p_btn" @click="getSubmit">
				<view>申请修改地址</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageNum: 1,
				listData: [],
				isLastPage: false, // 是否是最后一页
				loadingText: '',
				id_: '', // 选中的id
				orderId: '',
				detail: false,
			}
		},
		onLoad(e) {
			this.orderId = e.order_id;
			
			if(e.detail) {
				this.detail = e.detail;
			}
			
			this.getListData();
		},
		onReachBottom() {
			if (!this.isLastPage) {
				this.loadingText = '加载中...';
				this.pageNum++;
				this.getListData();
			} else {
				this.loadingText = '已加载全部';
			}
		},
		methods: {
			getListData() {
				this.$http.post(this.$api.addressList, {
					page: this.pageNum
				}).then(res => {
					this.listData = this.listData.concat(res.data.data);

					if (this.pageNum === res.data.last_page) {
						this.isLastPage = true;
						this.loadingText = '已加载全部';
					} else {
						this.isLastPage = false;
					}
				})
			},
			getItem(item) {
				if (item.province == '新疆维吾尔自治区' || item.province == '西藏自治区') {
					getApp().getMsg('新疆/西藏地区不可选');
					return
				}
				this.id_ = item.id;
			},
			getSubmit() {
				if (!this.id_) return getApp().getMsg('请选择地址');
				this.$http.post(this.$api.expressEdit, {
					address_id: this.id_,
					order_id: this.orderId
				}).then((res) => {
					getApp().getMsg('申请成功');
					getApp().getBack();
					
					if(this.detail) {
						uni.setStorageSync('editAdress', true);
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.p_img_128 {
		margin: 328rpx auto 32rpx;
	}

	.list_wrapper {
		display: flex;
		align-items: center;
		margin: 64rpx 0;
		padding: 0 32rpx;
	}
</style>
